package com.tgram.android_mapapi.map;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

/**
 * 
 * @author zk
 * @time 2014年9月22日 上午9:57:02
 * @classname LocationManager.java
 * @description 
 */
public class LocationManager {

	private static final String TAG = LocationManager.class.getSimpleName();

	private double localLontitude = -1;// 用于判断
	private static final int GPS_UNAVAILABLE = 1;
	private static final int GPS_WORKING = 2;
	private static LocationManager Instance;
	private OnLocationChanged mOnLocationChanged;
	private MyLocationListenner mLocationListener;

	private android.location.LocationManager locationManager;

	private Context mContext;
	private GpsTask mGpsTask;
	private String mProvider;

	private Timer timer;
	public static synchronized LocationManager getInstance(Context context) {
		if (Instance == null) {
			Instance = new LocationManager(context);
		}
		return Instance;
	}
	public LocationManager(Context context) {
		mContext = context;
	}

	/**
	 * 开始定位
	 */
	public void startLocation(OnLocationChanged onLocationChanged) {
		this.mOnLocationChanged = onLocationChanged;
		mLocationListener = new MyLocationListenner();
		// mLocationService.setLocationListener(this);
		getLocation();
		// mLocationService.start();
		timer = new Timer();
		mGpsTask = new GpsTask();
		// 5分钟后确定定位是否完成
		timer.schedule(mGpsTask, 5 * 60 * 1000);
	}

	//
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GPS_UNAVAILABLE:
				onPause();
				mOnLocationChanged.onLocationComplete(null);
				break;
			case GPS_WORKING:
				cancelGpsTask();
				break;
			}
			super.handleMessage(msg);
		}

	};

	/**
	 * google gps的相关设置
	 * 
	 */
	private void getLocation() {
		// 获取位置管理服务
		locationManager = (android.location.LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		// 查找到服务信息
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		// 高精度
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		// 低功耗
		mProvider = locationManager.getBestProvider(criteria, true);
		if (mProvider != null) {
			// Log.d("provider", "=" + mProvider);
			// 公安内网使用gps定位
			Location location = locationManager.getLastKnownLocation(android.location.LocationManager.GPS_PROVIDER);
			if (location != null) {
				updateToNewLocation(location);
			}
			// 设置监听器，自动更新的最小时间为间隔N秒(1秒为1*1000，这样写主要为了方便)或最小位移变化超过N米
			locationManager.requestLocationUpdates(mProvider, 5 * 1000, 10, mLocationListener);
		} else {
			Log.d("provider", "=null");
		}
	}

	public interface OnLocationChanged {
		/**
		 * 定位完成
		 */
		public void onLocationComplete(Location location);
	};

	/**
	 * gps信号正常,不用监测了
	 */
	private void cancelGpsTask() {
		if (mGpsTask != null) {
			mGpsTask.cancel();
			mGpsTask = null;
		}
	}

	/**
	 * 重启
	 */
	public void onResume() {
		mGpsTask = new GpsTask();
		timer.schedule(mGpsTask, 5 * 60 * 1000);
		if (mLocationListener != null) {
			locationManager.requestLocationUpdates(mProvider, 10 * 1000, 30, mLocationListener);
		}
	}

	/**
	 * 暂停,且关闭
	 */
	public void onPause() {
		cancelGpsTask();
		locationManager.removeUpdates(mLocationListener);
	}

	/**
	 * 更新位置信息
	 * 
	 * @param location
	 */
	private void updateToNewLocation(Location location) {
		if (location != null) {
			localLontitude = location.getLongitude();
			// 通知
			if (mOnLocationChanged != null) {
				mOnLocationChanged.onLocationComplete(location);
			}
		} else {
			mOnLocationChanged.onLocationComplete(null);
		}
	}

	/**
	 * 定位事件监听
	 */
	public class MyLocationListenner implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			updateToNewLocation(location);
		}

		// 当数据的提供者不能用的时候
		@Override
		public void onProviderDisabled(String provider) {
		}

		// 当数据的提供者能用的时候
		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			if (status == LocationProvider.OUT_OF_SERVICE || status == LocationProvider.TEMPORARILY_UNAVAILABLE) {
				mOnLocationChanged.onLocationComplete(null);
			}
		}
	}

	private class GpsTask extends TimerTask {

		@Override
		public void run() {
			Looper.prepare();
			if (localLontitude != -1) {
				handler.sendEmptyMessage(GPS_WORKING);
			} else {
				handler.sendEmptyMessage(GPS_UNAVAILABLE);
			}
			Looper.loop();
		}
	}

}
